#code to get confidence intervals for the ATT
#function for effect intervals
bootstrap_cum_effects_fixed <- function(boot_pre_list,
boot_post_list,
boot_pre_outcome_column,
outcome_post,
outcome_post_column,
lambda_min,
conf_int,
penalty = NULL) {
#Set the outcome columns, start and end columns
outcome_pre_column <- boot_pre_outcome_column
start_column <- boot_pre_outcome_column + 1
end_column <- ncol(boot_pre_list[[1]])
if(is.null(penalty)){
#apply the lasso to each bootstrapped sample to predict the treated with the non treated Columns
lasso_list <- purrr::map(boot_pre_list, function(df) (glmnet(x = df[,start_column:end_column], y = df[,outcome_pre_column],family="gaussian",
lower.limits=0, upper.limits =1, intercept=TRUE,standardize=FALSE,
alpha=1, nlambda = 100)))
}
if(!is.null(penalty)){
#apply the lasso to each bootstrapped sample to predict the treated with the non treated Columns
lasso_list <- purrr::map(boot_pre_list, function(df) (glmnet(x = df[,start_column:end_column], y = df[,outcome_pre_column],family="gaussian",
lower.limits=0, upper.limits =1, intercept=TRUE,standardize=FALSE,
penalty.factor = penalty,
alpha=1, nlambda = 100)))
}
#make predictions using the model with the old lambda for the post treatment period for each sample
pred_sim <- purrr::map2(lasso_list, boot_post_list,
function(.lasso_list, .boot_post_list) predict(.lasso_list, newx = .boot_post_list, s =lambda_min))
#Calculate the effects for each predicted series for the post period (oregon post - diff)
eff_sim <- purrr::map(pred_sim, function(diff) {(outcome_post[,outcome_post_column] - diff[,1])})
#get the cumulative effects - cumulative ssum Observed Oregon minus Predicted Oregon
cum_eff_sim <- purrr::map_dfc(eff_sim, function(cumulative){(cumsum(cumulative))})
#bind all the rows together
eff_sim <- do.call(cbind, eff_sim)
#calculate the cumulative effects intervals
#set the names for the effects table
table_names <- outcome_post[,1:(outcome_post_column - 1)]
#set the alpha
alpha = (1 - conf_int)/2
alpha_lower = 0 + alpha
alpha_higher = 1 - alpha
#calculate the effect intervals
eff_low = c(apply(eff_sim, 1, quantile, probs = alpha_lower))
eff_high = c(apply(eff_sim, 1, quantile, probs = alpha_higher))
average_eff = c(apply(eff_sim, 1, mean))
#cbind to make table
eff_table <- cbind.data.frame(table_names, eff_low, eff_high, average_eff)
#calculate the cumulative effects intervals
cum_pred_low = c(apply(cum_eff_sim, 1, quantile, probs = alpha_lower))
#Grab upper prediction/effects interval
cum_pred_high = c(apply(cum_eff_sim, 1, quantile, probs = alpha_higher))
#Get the average of the cumulative effects
average_cum_effect = c(apply(cum_eff_sim, 1, mean))
#cbind to make table
cum_effect_table <- cbind.data.frame(table_names,cum_pred_low, cum_pred_high, average_cum_effect)
#put the outcomes into a list
effects_list <- list(eff_sim, cum_eff_sim, eff_table, cum_effect_table)
#set the names
names(effects_list) <- c("effects","cum_effects",
"effects_intervals", "cum_effects_intervals")
#return the list
return(effects_list)
}
#grab data for bootstrapping
oregon_sim <- glmnet_data |>
dplyr::select(rowid, State, od_death_rate_half, nflis_fentanyl_percent_total)
#Grab 1000 bootstrap ensembles from the outcome column of the ODR_sim_matrix
set.seed(12112025)
#bootstrap of the outcome column for all 49 donor states
oregon_boot1 <-meboot::meboot(as.matrix(oregon_sim$od_death_rate_half), reps=1000, trim = list(trim=0.10, xmin=0, xmax = NULL))
#bootstrap of the percent fentanyl for Oregon
oregon_boot2 <-meboot::meboot(as.matrix(oregon_sim$nflis_fentanyl_percent_total), reps=1000, trim = list(trim=0.10, xmin=0, xmax = NULL))
#Make ensembles into a dataframe
oregon_boot1 <- oregon_boot1$ensemble |>
as.data.frame()
oregon_boot2 <- oregon_boot2$ensemble |>
as.data.frame()
#Add in the state and period columns and clean names
oregon_boot1 <- cbind(glmnet_data$rowid,glmnet_data$State, oregon_boot1) |>
dplyr::rename(rowid =1, state = 2)|>
dplyr::relocate(rowid,.before = 1)
#Add in the state and period columns and clean names
oregon_boot2 <- cbind(glmnet_data$rowid,glmnet_data$State, oregon_boot2) |>
dplyr::rename(rowid =1, state = 2)|>
dplyr::relocate(rowid,.before = 1) |>
filter(state == "Oregon")
#grab only our bootstrapped samples and convert to a list for the pre period
oregon_boot1_pre <- oregon_boot1 |>
filter(rowid < 27)
oregon_boot2_pre <- oregon_boot2 |>
filter(rowid < 27)
#make into a list for the pretreatment period
oregon1_boot_pre_list <- as.list(oregon_boot1_pre[,3:ncol(oregon_boot1_pre)])
oregon2_boot_pre_list <- as.list(oregon_boot2_pre[,3:ncol(oregon_boot2_pre)])
#add back in the column names to each object in the list and rename for pivoting
#for our entire ensemble series
oregon1_boot_pre_list <- purrr::map(oregon1_boot_pre_list, function(bind) (cbind.data.frame(oregon_boot1_pre$rowid, oregon_boot1_pre$state, bind))) |>
#rename the columns
purrr::map(dplyr::rename, rowid = 1, state = 2) |>
purrr::map(~tidyr::pivot_wider(.x, names_from = "state",
values_from = "bind"))|>
#reorder to make it easier for glmnet - Oregon in the second column
purrr::map(relocate, Oregon, .after = rowid) |>
#convert to a matrix for LASSO
purrr::map(as.matrix)
#Do the second covariate
oregon2_boot_pre_list <- purrr::map(oregon2_boot_pre_list, function(bind) (cbind.data.frame(oregon_boot2_pre$rowid, bind))) |>
#rename the columns
purrr::map(dplyr::rename, rowid = 1,fentanyl = 2)|>
#drop the the rowid
purrr::map(dplyr::select, -rowid)|>
#convert to a matrix for LASSO
purrr::map(as.matrix)
#bind both lists together
oregon_boot_pre_list <- purrr::map2(oregon1_boot_pre_list, oregon2_boot_pre_list, cbind)
#do the post period
#Make it into a list
oregon_boot1_post <- oregon_boot1 |>
filter(rowid >= 27)
oregon_boot2_post <- oregon_boot2 |>
filter(rowid >= 27)
#make into a list
oregon1_boot_post_list <- as.list(oregon_boot1_post[,3:ncol(oregon_boot1_post)])
oregon2_boot_post_list <- as.list(oregon_boot2_post[,3:ncol(oregon_boot2_post)])
#make this into a list
oregon1_boot_post_list <- purrr::map(oregon1_boot_post_list, function(bind) (cbind.data.frame(oregon_boot1_post$rowid, oregon_boot1_post$state, bind))) |>
#rename the columns
purrr::map(dplyr::rename, rowid = 1, state = 2) |>
purrr::map(~tidyr::pivot_wider(.x, names_from = "state",
values_from = "bind"))|>
#reorder to make it easier for glmnet - Oregon in the second column
purrr::map(relocate, Oregon, .after = rowid) |>
#drop the rowid
purrr::map(dplyr::select, -rowid, -Oregon)|>
#convert to a matrix for LASSO
purrr::map(as.matrix)
#Do the second covariate
oregon2_boot_post_list <- purrr::map(oregon2_boot_post_list, function(bind) (cbind.data.frame(oregon_boot2_post$rowid, bind))) |>
#rename the columns
purrr::map(dplyr::rename, rowid = 1,fentanyl = 2)|>
#drop the state
purrr::map(dplyr::select, -rowid)|>
#convert to a matrix for LASSO
purrr::map(as.matrix)
#bind both lists together
oregon_boot_post_list <- purrr::map2(oregon1_boot_post_list, oregon2_boot_post_list, cbind)
#create the post period
Oregon_post_observed <- glmnet_data |>
select(rowid, State, od_death_rate_half) |>
filter(State == 'Oregon') |>
filter(rowid >= 27)